package com.yxcz.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yxcz.model.AddRoleForm;
import com.yxcz.model.Position;
import com.yxcz.model.Role;
import com.yxcz.model.UpdateRoleForm;
import com.yxcz.service.PositionService;

@Controller
@RequestMapping("/Position")
public class PositionController {
	
	@Resource
	private PositionService positionService;
	
	@RequestMapping(value="index")
	public ModelAndView index(ModelAndView modelAndView){
		
		modelAndView.addObject("positions",positionService.list());
		modelAndView.setViewName("/Manager/Position");
		return modelAndView;
		
	}
	
	@RequestMapping(value="getPosition")
	@ResponseBody
	public Position getPosition(Integer id){
		
		if (id == null)
			return null;

		Position position = positionService.getPosition(id);

		return position;
		
	}
	
	@RequestMapping(value = "deleteById")
	public ModelAndView deleteById(Integer id, ModelAndView modelAndView) {
		if (id == null) {
			modelAndView.setViewName("redirect:/Common/500Page");
		}

		else {
			positionService.deleteById(id);
			modelAndView.addObject("msg", "删除");
			modelAndView.setViewName("Common/success");
		}
		return modelAndView;
	}
	
	
	@RequestMapping(value = "update")
	public ModelAndView update(@Validated Position form, BindingResult bindingResult, ModelAndView modelAndView) {
		if (bindingResult.hasErrors()) {
			modelAndView.setViewName("redirect:/Common/500Page");
		} else {
			positionService.update(form);
			modelAndView.addObject("msg", "修改");
			modelAndView.setViewName("Common/success");
		}
		return modelAndView;
	}
	
	@RequestMapping(value = "add")
	public ModelAndView add(@Validated Position form, BindingResult bindingResult, ModelAndView modelAndView) {
		if (bindingResult.hasErrors()) {
			modelAndView.setViewName("redirect:/Common/500Page");
		} else {
			try {
				positionService.add(form);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				modelAndView.setViewName("redirect:/Common/500Page");
				return modelAndView;
			}
			modelAndView.addObject("msg", "增加");
			modelAndView.setViewName("Common/success");
		}
		return modelAndView;
	}


}
